############ Read in all the ind. pct files
# Change this file path
filepathlad="H:/Zheng_10223/Derived Data/LAD/"


for(yy in 1982:2019){
  
  agelist=lapply(17:80, function(age){
    
    filein=paste(filepathlad,"ind/indpct",yy,"_age",age,".csv",sep="")
    
    inddist=read.csv(filein, header=TRUE, stringsAsFactors = FALSE)
    inddist$countobvs_ind=inddist$countobvs; inddist$countobvs=NULL 
    
    return(inddist)
  })
  
  
  
  # FILE OF all ages 
  ageout=do.call(rbind, agelist)
  
  ageout$year=yy
  
  if(yy==1982){
    
    inddist_out=ageout
  }
  else{
    inddist_out=rbind(ageout,inddist_out)
  }
  
  
}  

write.csv(inddist_out, paste(filepathlad, "ind_income_dist_1982_2019.csv",sep=""))

ageout=NULL  
############# Parent 

for(yy in 1982:2019){
  
  agelist=lapply(17:80, function(age){
    
    filein=paste(filepathlad,"parents/parentpct",yy,"_age",age,".csv",sep="")
    
    parentdist=read.csv(filein, header=TRUE, stringsAsFactors = FALSE)
    parentdist$countobvs_parent=parentdist$countobvs; parentdist$countobvs=NULL 
    
    return(parentdist)
  })
  
  
  
  # FILE OF all ages 
  ageout=do.call(rbind, agelist)
  
  ageout$year=yy
  
  if(yy==1982){
    
    parentdist_out=ageout
  }
  else{
    parentdist_out=rbind(ageout,parentdist_out)
  }
  
  
}  

write.csv(parentdist_out, paste(filepathlad, "parent_income_dist_1982_2019.csv",sep=""))


###########################
# Household 

for(yy in 1982:2019){
  
  agelist=lapply(17:80, function(age){
    
    filein=paste(filepathlad,"/hh/hhpct",yy,"_maxspouseage",age,".csv",sep="")
    
    hhdist=read.csv(filein, header=TRUE, stringsAsFactors = FALSE)
    hhdist$countobvs_hh=hhdist$countobvs; hhdist$countobvs=NULL 
    hhdist$countfam=NULL 
    
    return(hhdist)
  })
  
  
  
  
  ageout=do.call(rbind, agelist)
  ageout$year=yy
  
  if(yy==1982){
    
    HHdist=ageout
  }
  else{
    HHdist=rbind(ageout,HHdist)
  }
  
  
}  

write.csv(HHdist,paste(filepathlad, "hh_income_dist_maxspouseage_1982_2019.csv",sep=""))


# merge:
HHdist$age=HHdist$maxage
finaldist=merge(HHdist, inddist_out, by=c("pct","age","year"))
finaldist$maxage=NULL
finaldist=merge(finaldist, parentdist_out, by=c("pct","age","year"))

write.csv(finaldist,paste(filepathlad,"finaldist.csv",sep=""))

